
package presentacion;

import java.util.LinkedList;
import java.util.Properties;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;
import negocio.control.ServiciosDeCorreo;
import negocio.entidades.ListaDeCorreos;

/**
 *
 * @author Elias
 */
public class IGListaDeCorreos extends javax.swing.JFrame {

    /** Creates new form IGListaDeCorreos */
    public IGListaDeCorreos() {
        inicializarLista();
        initComponents();
        System.out.println("ready");
    }

    private void inicializarLista(){
        listasDeCorreos = ServiciosDeCorreo.consultarListasDeCorreo();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        nueva = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        listas = new javax.swing.JTable();
        eliminar = new javax.swing.JButton();
        elimReg = new javax.swing.JButton();
        aceptar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel2.setText("Listas de Correos");

        nueva.setFont(new java.awt.Font("Tahoma", 0, 12));
        nueva.setText("Nueva lista");
        nueva.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nuevaActionPerformed(evt);
            }
        });

        listas.setModel(new ModeloTabla());
        jScrollPane1.setViewportView(listas);

        eliminar.setFont(new java.awt.Font("Tahoma", 0, 12));
        eliminar.setText("Eliminar lista");
        eliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eliminarActionPerformed(evt);
            }
        });

        elimReg.setFont(new java.awt.Font("Tahoma", 0, 12));
        elimReg.setText("NoReceptores");
        elimReg.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                elimRegActionPerformed(evt);
            }
        });

        aceptar.setText("Aceptar");
        aceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aceptarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 531, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(nueva, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(41, 41, 41)
                                .addComponent(eliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 43, Short.MAX_VALUE)
                                .addComponent(elimReg, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel2))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(470, Short.MAX_VALUE)
                        .addComponent(aceptar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nueva, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(elimReg, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(eliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 381, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(aceptar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void nuevaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nuevaActionPerformed
        NuevaLista dialogo = new NuevaLista(this, true);
        dialogo.setVisible(true);
}//GEN-LAST:event_nuevaActionPerformed

    private void eliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eliminarActionPerformed
        int seleccionada = listas.getSelectedRow();
        if(seleccionada >= 0){
            String nombre = listasDeCorreos.get(seleccionada).getNombre();
            ServiciosDeCorreo.eliminarLista(nombre);
            inicializarLista();
            listas.setModel(new ModeloTabla());
        }else{
            JOptionPane.showMessageDialog(this,
                                    "Debe seleccionar una lista antes de realizar esta acción.",
                                    "Atención",
                                    JOptionPane.INFORMATION_MESSAGE);
        }
}//GEN-LAST:event_eliminarActionPerformed

    private void elimRegActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_elimRegActionPerformed
        int seleccionada = listas.getSelectedColumn();
        if(seleccionada >= 0){
            EliminarRegistros dialogo = new EliminarRegistros(this,true);
            dialogo.setVisible(true);
        }else{
            JOptionPane.showMessageDialog(this,
                                    "Debe seleccionar una lista antes de realizar esta acción.",
                                    "Atención",
                                    JOptionPane.INFORMATION_MESSAGE);
        }
}//GEN-LAST:event_elimRegActionPerformed

    private void aceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aceptarActionPerformed
        this.dispose();
}//GEN-LAST:event_aceptarActionPerformed


    private class ModeloTabla extends AbstractTableModel{

        @Override
        public int getRowCount() {
            return listasDeCorreos.size();
        }

        @Override
        public int getColumnCount() {
            return 1;
        }

        @Override
        public String getColumnName(int column) {
            switch(column){
                case 0:{
                    return "Nombre";
                }
                default:{
                    return "--";
                }
            }
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            switch(columnIndex){
                case 0:{
                    return listasDeCorreos.get(rowIndex).getNombre();
                }
                default:{
                    return "--";
                }
            }
        }
    }

    //Metodos de dialogos
    protected void setNombreNuevaLista(String nombre){
        nombreNuevaLista = nombre;
    }

//    protected void setOringenDeDatos(negocio.entidades.OrigenDeDatos origen){
//        if(nuevaLista != null){
//            nuevaLista.setOrigenDeDatos(origen);
////            ServiciosDeCorreo.guardarLista(nuevaLista, columnas, asunto, mensaje, adjuntos);
//        }
//    }

    protected void eliminarRegistros(String[] correosAEliminar){
        int seleccionada = listas.getSelectedColumn();
        ListaDeCorreos lista;
        if(seleccionada >= 0){
            lista = listasDeCorreos.get(seleccionada);
            ServiciosDeCorreo.eliminarRegistrosdeLista(lista, correosAEliminar);
        }
    }

    protected void crearLista(int origen, Properties datos){
        nuevaLista = ServiciosDeCorreo.crearListaDeCorreos(nombreNuevaLista, origen, datos);
    }

    protected String[] getColumnasDisponibles(){
        return ServiciosDeCorreo.columnasDisponibles(nuevaLista.getOrigenDeDatos().getComportamientoOrigen());
    }

    protected void guardarLista( Properties columnas, String asunto, String mensaje, String[] adjuntos){
        ServiciosDeCorreo.guardarLista(nuevaLista,columnas,asunto,mensaje,adjuntos);
        inicializarLista();
        listas.setModel(new ModeloTabla());
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {

        try{
            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
        }
        catch(Exception ex){
            //Sin comentarios
        }

        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new IGListaDeCorreos().setVisible(true);
            }
        });
    }

    //Variables de vista:
    private LinkedList<ListaDeCorreos> listasDeCorreos;
    private ListaDeCorreos nuevaLista;
    private String nombreNuevaLista;

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton aceptar;
    private javax.swing.JButton elimReg;
    private javax.swing.JButton eliminar;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable listas;
    private javax.swing.JButton nueva;
    // End of variables declaration//GEN-END:variables

}
